﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.logic;

namespace com.lover.astd.common.logicexe.activities
{
    public class TreasureGameExe : ExeBase
    {
        public TreasureGameExe() { _name = ConfigStrings.S_Treasure_Game; _readable = ConfigStrings.SR_Treasure_Game; }
        public override long execute()
        {
            int _minGoldSteps = 0;
            string _open_box_type = "", _gold_move_box_type = "";
            Dictionary<string, string> conf = getConfig();
            if (!conf.ContainsKey(ConfigStrings.enabled) || !conf[ConfigStrings.enabled].ToLower().Equals("true")) return next_day();
            if (conf.ContainsKey(ConfigStrings.goldstep)) Int32.TryParse(conf[ConfigStrings.goldstep], out _minGoldSteps);
            if (conf.ContainsKey(ConfigStrings.open_box_type)) _open_box_type = conf[ConfigStrings.open_box_type];
            if (conf.ContainsKey(ConfigStrings.gold_move_box_type)) _gold_move_box_type = conf[ConfigStrings.gold_move_box_type];

            bool use_ticket = conf.ContainsKey(ConfigStrings.use_ticket) && conf[ConfigStrings.use_ticket].ToLower().Equals("true");
            int ticket_type = 2;
            if (conf.ContainsKey(ConfigStrings.use_ticket_type)) Int32.TryParse(conf[ConfigStrings.use_ticket_type], out ticket_type);
            if (ticket_type > 3 || ticket_type < 1) ticket_type = 2;
            bool gold_end = conf.ContainsKey(ConfigStrings.ticket_gold_end) && conf[ConfigStrings.ticket_gold_end].ToLower().Equals("true");

            ActivityMgr mgr = ServiceFactory.getActivityManager();
            int gold_available = getGoldAvailable();
            int result = mgr.handleTreasureGameInfo(_proto, _logger, _user, _open_box_type, _gold_move_box_type, _minGoldSteps, ref gold_available);
            if (result == 10) return next_day();
            if (result == 1) return next_hour();
            if (result == 2)
            {
                if (use_ticket)
                {
                    int new_result = mgr.handleNewTreasureGames(_proto, _logger, getGoldAvailable(), ticket_type, gold_end);
                    if (new_result == 1) return next_halfhour();
                    else if (new_result == 0) return immediate();
                    else return next_hour();
                }
                return next_hour();
            }
            if (result == 3)
            {
                if (use_ticket)
                {
                    int new_result = mgr.handleNewTreasureGames(_proto, _logger, getGoldAvailable(), ticket_type, gold_end);
                    if (new_result == 1) return next_halfhour();
                    else if (new_result == 0) return immediate();
                    else return next_day();
                }
                return next_day();
            }
            return immediate();
        }
    }
}
